home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap06 / DlgDemo1 / DlgDemo1.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  2.6 KB  |  108 lines

  1. //***********************************************************************
  2. //
  3. //  DLGDEMO1.CPP
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include "Resource.h"
  9. #include "DlgDemo1.h"
  10.  
  11. #define FONTHEIGHT 72
  12.  
  13. CMyApp myApp;
  14.  
  15. /////////////////////////////////////////////////////////////////////////
  16. // CMyApp member functions
  17.  
  18. BOOL CMyApp::InitInstance ()
  19. {
  20.     m_pMainWnd = new CMainWindow;
  21.     m_pMainWnd->ShowWindow (m_nCmdShow);
  22.     m_pMainWnd->UpdateWindow ();
  23.     return TRUE;
  24. }
  25.  
  26. /////////////////////////////////////////////////////////////////////////
  27. // CMainWindow message map and member functions
  28.  
  29. BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
  30.     ON_WM_ERASEBKGND ()
  31.     ON_WM_PAINT ()
  32.     ON_COMMAND (IDM_OPTIONS_EXIT, OnOptionsExit)
  33.     ON_COMMAND (IDM_OPTIONS_ABOUT, OnOptionsAbout)
  34. END_MESSAGE_MAP ()
  35.  
  36. CMainWindow::CMainWindow ()
  37. {
  38.     Create (NULL, "DlgDemo1", WS_OVERLAPPEDWINDOW, rectDefault, NULL,
  39.         MAKEINTRESOURCE (IDR_MAINFRAME));
  40. }
  41.  
  42. BOOL CMainWindow::OnEraseBkgnd (CDC* pDC)
  43. {
  44.     CRect rect;
  45.     GetClientRect (&rect);
  46.     DoGradientFill (pDC, &rect);
  47.     return TRUE;
  48. }
  49.  
  50. void CMainWindow::OnPaint ()
  51. {
  52.     CRect rect;
  53.     GetClientRect (&rect);
  54.  
  55.     CPaintDC dc (this);
  56.     DoDrawText (&dc, &rect);
  57. }
  58.  
  59. void CMainWindow::OnOptionsExit ()
  60. {
  61.     SendMessage (WM_CLOSE, 0, 0);
  62. }
  63.  
  64. void CMainWindow::OnOptionsAbout ()
  65. {
  66.     CDialog dlg (IDD_ABOUTDLG, this);
  67.     dlg.DoModal ();
  68. }
  69.  
  70. void CMainWindow::DoGradientFill (CDC* pDC, CRect* pRect)
  71. {
  72.     CPen* pPen[64];
  73.     for (int i=0; i<64; i++)
  74.         pPen[i] = new CPen (PS_SOLID, 1, RGB (0, 0, 255 - (i * 4)));
  75.  
  76.     int nWidth = pRect->Width ();
  77.     int nHeight = pRect->Height ();
  78.  
  79.     for (i=0; i<nHeight; i++) {
  80.         pDC->SelectObject (pPen[(i * 63) / nHeight]);
  81.         pDC->MoveTo (0, i);
  82.         pDC->LineTo (nWidth, i);
  83.     }
  84.  
  85.     pDC->SelectStockObject (BLACK_PEN);
  86.     for (i=0; i<64; i++)
  87.         delete pPen[i];
  88. }
  89.  
  90. void CMainWindow::DoDrawText (CDC* pDC, CRect* pRect)
  91. {
  92.     CFont font;
  93.     int nHeight = -((pDC->GetDeviceCaps (LOGPIXELSY) * FONTHEIGHT) / 72);
  94.  
  95.     font.CreateFont (nHeight, 0, 0, 0, FW_BOLD, TRUE, 0, 0,
  96.         DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
  97.         DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Times New Roman");
  98.  
  99.     pDC->SetBkMode (TRANSPARENT);
  100.     pDC->SetTextColor (RGB (255, 255, 255));
  101.  
  102.     CFont* pOldFont = pDC->SelectObject (&font);
  103.     pDC->DrawText ("Hello, MFC", -1, pRect, DT_SINGLELINE | DT_CENTER |
  104.         DT_VCENTER);
  105.  
  106.     pDC->SelectObject (pOldFont);
  107. }
  108.